package at.spengergasse.web.servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

@SuppressWarnings("serial")
public class HelloWorldServlet extends HttpServlet {

  private static final Log log = LogFactory.getLog(HelloWorldServlet.class);

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    /**
     * <HTML> <HEAD> </HEAD> <BODY> <H1>Hello Servlet World!</H1> </BODY>
     * </HTML>
     */
    log.debug("started processing of doGet");

    response.getWriter().write("<HTML>");
    response.getWriter().write("<HEAD>");
    response.getWriter().write("</HEAD>");
    response.getWriter().write("<BODY>");
    response.getWriter().write("<H1>Hello Servlet World!</H1>");
    response.getWriter().write(
        "The Server Time is: " + new Date() + "(" + System.currentTimeMillis()
            + ")<BR/><BR/>");

    if (request.getParameterMap().keySet().size() > 0) {
      response.getWriter().write("Parameters were: <br/>");
      for (Object paramName : request.getParameterMap().keySet()) {
        String parameter = request.getParameter((String) paramName);
        response.getWriter().write(paramName + ": " + parameter + "<br/>");
      }
    }

    response.getWriter().write("</BODY>");
    response.getWriter().write("</HTML>");
    response.getWriter().flush();
    response.getWriter().close();

    log.debug("finished processing of doGet");
  }

}
